﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using CRMMis.BLL;

public partial class CRM_Manager_ERP_CargoRelation : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            StoreBind();
        }
    }

    #region 绑定数据

    private void StoreBind()
    {
        DataTable dt = ERP_Customer.GetList("1=1").Tables[0];
        store_Custom.DataSource = dt;
        store_Custom.DataBind();
        if (dt.Rows.Count > 0)
            combox_CustomId.SelectedItem.Value = dt.Rows[0]["CustomerId"].ToString();
    }

    protected void MyData_Refresh(object sender, StoreRefreshDataEventArgs e)
    {
        DataTable dt = GetDataTable();
        if (dt != null)
        {
            storeCargo.DataSource = dt;
            storeCargo.DataBind();
        }
    }

    protected void MyData_Refresh2(object sender, StoreRefreshDataEventArgs e)
    {
        if (combox_CustomId.SelectedItem.Text == "")
        {
            return;
        }
        storeCusCargo.DataSource = GetDeptDataTable();
        storeCusCargo.DataBind();
    }

    private DataTable GetDeptDataTable()
    {
        string strsql = "  State=1 and CustomId='" + combox_CustomId.SelectedItem.Value + "'";
        if (exttxtf_HisCharge.Text != "")
            strsql += " and (CargoName like '%" + exttxtf_HisCharge.Text.Trim() + "%'  or Spell like '%" + exttxtf_HisCharge.Text.Trim() + "%' or ByName like '%" + exttxtf_HisCharge.Text.Trim() + "%' or CargoCode like '%" + exttxtf_HisCharge.Text.Trim() + "%')";
        DataTable dt = ERP_CusCargo_Detail.GetList(strsql).Tables[0];
        return dt;
    }

    private DataTable GetDataTable()
    {
        string strsql = " State=1";
        if (exttxtf_Query.Text != "")
            strsql += " and (CargoName like '%"+ exttxtf_Query.Text.Trim() + "%'  or Spell like '%" + exttxtf_Query.Text.Trim() + "%' or ByName like '%" + exttxtf_Query.Text.Trim() + "%' or CargoCode like '%" + exttxtf_Query.Text.Trim() + "%')";
        DataTable dt = ERP_Cargo_Detail.GetList(strsql).Tables[0];
        return dt;
    }

    #endregion

    [DirectMethod]
    public void TextKeyPress()
    {
        gridCargo.Reload();
    }

    [DirectMethod]
    public void TextKeyPress1()
    {
        gridCusCargo.Reload();
    }

    [DirectMethod]
    public void LoadRelation(string CargoId, string CusCargoId, string CustomerId)
    {
        DataTable dt = ERP_CgRelations.GetList("CargoId=" + CusCargoId + " and BCargoId=" + CargoId + " and CustomerId=" + CustomerId + "", "RState desc").Tables[0];
        StoreRelations.DataSource = dt;
        StoreRelations.DataBind();
    }

    [DirectMethod]
    public void MappingMat(string CargoId,string DetailId, string CusCargoId,string CusDetailId,string CustomerId)
    {
        if (string.IsNullOrEmpty(CargoId) || string.IsNullOrEmpty(CusCargoId))
            return;
        CRMMis.Model.ERP_CgRelations model = new CRMMis.Model.ERP_CgRelations();
        DataTable dt1 = ERP_CgRelations.GetList("BCargoId=" + CargoId + " and CargoId="+ CusCargoId + " and CustomerId=" + CustomerId + "").Tables[0];
        foreach (DataRow dr in dt1.Rows)
        {
            model = ERP_CgRelations.GetModel(int.Parse(dr["Relationid"].ToString()));
            model.state = 0;
            ERP_CgRelations.Update(model);
        }
        DataTable dt = ERP_CgRelations.GetList("BCargoId=" + CargoId + " and CargoId="+ CusCargoId + " and CustomerId=" + CustomerId + " and OwerDetailId="+ DetailId + " and CusDetailId="+ CusDetailId + " and RState=0").Tables[0];
        if (dt.Rows.Count > 0)
        {
            model = ERP_CgRelations.GetModel(int.Parse(dt.Rows[0]["Relationid"].ToString()));
            model.state = 1;
            ERP_CgRelations.Update(model);
        }
        else
        {
            model.Relationid = ERP_CgRelations.GetNewID();
            model.CargoId = int.Parse(CargoId);
            model.state = 1;
            model.CustomerId = int.Parse(CustomerId);
            model.CusCargoId = int.Parse(CusCargoId);
            model.OwerDetailId = int.Parse(DetailId);
            model.CusDetailId = int.Parse(CusDetailId);
            ERP_CgRelations.Add(model);
        }
        string handler = "";
        handler = "SelectedCargo();";
        X.Msg.Alert("系统提示", "操作成功！", handler).Show();
    }

    [DirectMethod]
    public void CancelMat(string CargoId, string CusCargoId, string CustomerId)
    {
        if (string.IsNullOrEmpty(CargoId) || string.IsNullOrEmpty(CusCargoId))
            return;
        CRMMis.Model.ERP_CgRelations model = new CRMMis.Model.ERP_CgRelations();
        DataTable dt = ERP_CgRelations.GetList("BCargoId='" + CargoId + "' and CargoId='" + CusCargoId + "' and CustomId='"+CustomerId+"'").Tables[0];
        model = ERP_CgRelations.GetModel(int.Parse(dt.Rows[0]["Relationid"].ToString()));
        model.state = 0;
        ERP_CgRelations.Update(model);
        //GridPanel2.Reload();
    }
}